package com.demo.user.service.biz;

import com.demo.user.constant.MenuConstant;
import com.demo.user.entity.AuthMenu;
import com.demo.user.entity.AuthRoleMenu;
import com.demo.user.entity.AuthUser;
import com.demo.user.entity.AuthUserRole;
import com.demo.user.entity.table.AuthMenuTableDef;
import com.demo.user.entity.table.AuthRoleMenuTableDef;
import com.demo.user.entity.table.AuthUserRoleTableDef;
import com.demo.user.entity.table.AuthUserTableDef;
import com.demo.user.service.AuthMenuService;
import com.demo.user.service.AuthRoleMenuService;
import com.demo.user.service.AuthUserRoleService;
import com.demo.user.service.AuthUserService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DayDayUp
 * create on 2023-08-15
 **/
@Slf4j
@Service
public class AuthUserMenuService {
    @Resource
    private AuthMenuService authMenuService;
    @Resource
    private AuthRoleMenuService authRoleMenuService;
    @Resource
    private AuthUserRoleService authUserRoleService;
    @Resource
    private AuthUserService authUserService;

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public List<String> getButtonListByUsername(String username) {
        // 查询用户
        QueryWrapper userQueryWrapper = QueryWrapper.create();
        userQueryWrapper.where(AuthUserTableDef.AUTH_USER.USERNAME.eq(username));
        AuthUser authUser = authUserService.getOne(userQueryWrapper);

        // 根据用户ID查询角色ID
        QueryWrapper userRoleQueryWrapper = QueryWrapper.create();
        userRoleQueryWrapper.where(AuthUserRoleTableDef.AUTH_USER_ROLE.USER_ID.eq(authUser.getId()));
        List<AuthUserRole> authUserRoleList = authUserRoleService.list(userRoleQueryWrapper);

        // 获取角色ID集合
        List<Long> roleIdList = authUserRoleList.stream().map(AuthUserRole::getRoleId).distinct().collect(Collectors.toList());

        // 根据角色ID获取菜单ID集合
        QueryWrapper roleMenuQueryWrapper = QueryWrapper.create();
        roleMenuQueryWrapper.where(AuthRoleMenuTableDef.AUTH_ROLE_MENU.ROLE_ID.in(roleIdList));
        List<AuthRoleMenu> authRoleMenuList = authRoleMenuService.list(roleMenuQueryWrapper);

        if (CollectionUtil.isEmpty(authRoleMenuList)) {
            return Collections.emptyList();
        }

        // 获取菜单ID集合
        List<Long> menuIdList = authRoleMenuList.stream().map(AuthRoleMenu::getMenuId).distinct().collect(Collectors.toList());

        // 获取菜单集合
        QueryWrapper menuQueryWrapper = QueryWrapper.create();
        menuQueryWrapper.where(AuthMenuTableDef.AUTH_MENU.ID.in(menuIdList));
        menuQueryWrapper.and(AuthMenuTableDef.AUTH_MENU.TYPE.eq(MenuConstant.BUTTON));
        List<AuthMenu> authMenuList = authMenuService.list(menuQueryWrapper);

        if (CollectionUtil.isEmpty(authMenuList)) {
            return Collections.emptyList();
        }

        // 获取按钮集合
        return authMenuList.stream().map(AuthMenu::getMenuCode).collect(Collectors.toList());
    }
}
